package com.example.demo.wuliu;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.datasource.multi.dao.wuliuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static java.lang.Math.*;

@Service
public class wuliuservice {

    @Autowired
    private com.example.demo.datasource.multi.dao.wuliuDao wuliuDao;

    public JSONObject qurywuliu(double length, double wight, double height, String sendcity, String receiveprovince,
                                String receivecity, double asw,String selectvalue){

        //对所要查询的字符串进行拼接,然后将sql作为参数传入dao层
        String  gsname = "SELECT a.wuliuname,a.lctparabolic from lctp a where a.status = 1" ;
        List<wuliugspojo>  gslist = wuliuDao.qurygsname(gsname);
        System.out.println(gslist);
        TreeMap<String, Double> result = new TreeMap<String, Double>();
        TreeMap<String, String> result3 = new TreeMap<String, String>();
        /*Map<String,String> result=new HashMap<>();
        double[] array = new double[gslist.size()];
        int iii=0;*/

        for (wuliugspojo gswlp: gslist
             ) {
            String name = gswlp.getWuliuname();
            int lctparabolic = gswlp.getLctparabolic();
            String  sql = "select a.deliveryname,a.fww,a.fwp,a.wu,a.wd,a.wp,a.fw,a.discounttype,a.discount,b.lctparabolic,c.aswruleCode,c.aswruleName,c.logicCode,c.logicName " +
                    "from wuliuyunfei a " +
                    "LEFT JOIN lctp b ON a.code = b.wuliucode " +
                    "LEFT JOIN calculaterule c ON c.wuliucode = b.wuliucode " +
                    "WHERE a.sendcity = '" + sendcity +
                    "' and a.receiveprovince = '" +receiveprovince +
                    "' and a.receivecity = '" + receivecity +
                    "' and a.deliveryname = '" + name +
                    "' and a.flag = '" + selectvalue + "'";
            List<wuliupojo>  list = wuliuDao.qurywuliu(sql);
            System.out.println(list);
            double result1 = getyuguV2(list,length,wight,height,asw,lctparabolic,name);
            System.out.println(result1);
            if (result1 == 0.0){
                result3.put(name, "计算错误！可能为首重等参数异常");
            }else if (result1 == -1.0){
                result3.put(name,"没有这条路线,请检查原寄城市");
            }else {
                /*String S = String.valueOf((long)result1);
                System.out.println(S);*/
                result.put(name, result1);
                /*array[iii]=result1;
                iii++;*/
            }
        }
        System.out.println(result);
        System.out.println(result.size());
        if (result.size() == 0){
            Map<String,Object> result1= new HashMap<>();
            result1.put("msg","0");
            result1.put("falseresult",result3);
            JSONObject result2 = JSONObject.parseObject(JSON.toJSONString(result1));
            return result2;
        }
        /*Arrays.sort(array); //升序排序
        System.out.println(array[0]);
        String resultname = null;

        for(String key: result.keySet()){
            Double value = result.get(key); //遍历map的key
            if (value == array[0]){
                System.out.println(key);
                System.out.println(value);
                resultname = key;

            }
        }*/

        List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(result.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String, Double>>() {
            //Map.Entry升序排序
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        System.out.println(list);
        System.out.println(list.get(0).getKey());

        /*//LinkedHashMap排序
        Map<String, String> finalMap = new LinkedHashMap<>();
        // Sort a map and add to finalMap
        result.entrySet().stream().sorted(Map.Entry.<String, String>comparingByKey().reversed())
                .forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
        System.out.println(finalMap);*/



        Map<String,Object> result1= new HashMap<>();
        result1.put("resultname",list.get(0).getKey()); // 推荐的快递名称
        result1.put("resultvalue", list.get(0).getValue()); // 推荐的快递运费
        result1.put("msg","1");
        result1.put("result",list);
        result1.put("falseresult",result3);
        JSONObject result2 = JSONObject.parseObject(JSON.toJSONString(result1));

        return result2;
    }


    public double getyuguV2(List<wuliupojo> wlps,double length, double wight, double height, double asw,int lctparabolic,String name){
        if (wlps.size() == 0){
            return -1.0;
        }//没有这条快递路线

        if(wlps.get(0).getFwp() == 0.0){
            return 0.0;
        }; //没有首重价格



        double volume; //体积
        double volumeWight; //体积重量
        double chargeableWeight; //计费重量
        double acw = 0; //实际计费重量

        /*体积*/
        volume=new BigDecimal(length*wight*height).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
        System.out.println("体积："+volume);

        /*体积重量*/
        volumeWight= new BigDecimal(
                //((volume/Cast_light_coefficient)/1000)
                new BigDecimal(new BigDecimal(volume).divide(new BigDecimal(lctparabolic),4,BigDecimal.ROUND_HALF_UP).doubleValue())
                        .divide(new BigDecimal(1000),4,BigDecimal.ROUND_HALF_UP)
                        .doubleValue()
        ).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("体积重量："+volumeWight);

        /*计费重量*/
        chargeableWeight=max(volumeWight, asw);
        System.out.println("计费重量："+chargeableWeight);

        /*实际计费重量*/
        if (wlps.get(0).getAswruleName().equals("顺丰通用")){
            System.out.println(name);
            if (chargeableWeight >= 100){
                acw = round(chargeableWeight);
                System.out.println("实际计费重量："+acw);

            }else if (chargeableWeight >=10){
                //IF(K4>=10,ROUND(K4*2,0)/2,ROUND(K4,1))
                acw = new BigDecimal(round(chargeableWeight*2)).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP).doubleValue();//除
                System.out.println("实际计费重量："+acw);

            }else {
                acw = new BigDecimal(chargeableWeight).setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留一位小数
                System.out.println("实际计费重量："+acw);

            }
        }else if (wlps.get(0).getAswruleName().equals("京东通用")){
            System.out.println(name);
            double zh = ceil(chargeableWeight); //向上取整
            double yu = zh - chargeableWeight;  //取到小数
            //System.out.println(new BigDecimal(zh).subtract(new BigDecimal(chargeableWeight)).doubleValue());
            System.out.println("向上区间："+yu);
            if (yu > 0.5){
                yu = 0.5;
                double fl = floor(chargeableWeight); //向下取整
                acw = fl + yu;  //向上取得0.5的倍数
                System.out.println("实际计费重量："+acw);
            }else if (yu < 0.5){
                acw = zh;
                System.out.println("实际计费重量："+acw);
            }else if (yu == 0.5){
                acw = chargeableWeight;
                System.out.println("实际计费重量："+acw);
            }

        }else if (wlps.get(0).getAswruleName().equals("默认")){
            acw = round(chargeableWeight);
            System.out.println("实际计费重量："+acw);
        }else if (wlps.get(0).getAswruleName().equals("四舍五入小数1")){
            acw = new BigDecimal(chargeableWeight).setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留一位小数
            System.out.println("实际计费重量："+acw);
        }else if (wlps.get(0).getAswruleName().equals("四舍五入小数2")){
            acw = new BigDecimal(chargeableWeight).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留2位小数
            System.out.println("实际计费重量："+acw);
        }else if (wlps.get(0).getAswruleName().equals("向上取整")){
            acw = ceil(chargeableWeight); //向上取整
            System.out.println("实际计费重量："+acw);
        }else if (wlps.get(0).getAswruleName().equals("向下取整")){
            acw = floor(chargeableWeight); //向下取整
            System.out.println("实际计费重量："+acw);
        }else if (wlps.get(0).getAswruleName().equals("向上取0.5倍数")){
            double zh = ceil(chargeableWeight); //向上取整
            double yu = zh - chargeableWeight;  //取到小数
            //System.out.println(new BigDecimal(zh).subtract(new BigDecimal(chargeableWeight)).doubleValue());
            System.out.println("向上区间："+yu);
            if (yu > 0.5){
                yu = 0.5;
                double fl = floor(chargeableWeight); //向下取整
                acw = fl + yu;  //向上取得0.5的倍数
                System.out.println("实际计费重量："+acw);
            }else if (yu < 0.5){
                acw = zh;
                System.out.println("实际计费重量："+acw);
            }else if (yu == 0.5){
                acw = chargeableWeight;
                System.out.println("实际计费重量："+acw);
            }
        }else {
            acw = round(chargeableWeight);
            System.out.println("实际计费重量："+acw);
        }
        double yuguprice = 0;

        if (wlps.get(0).getLogicName().equals("顺丰标快")){

            wuliupojo wl = wlps.get(wlps.size()-1);
            wuliupojo wl1 = wlps.get(wlps.size()-2);
            double wd1 = wl.getWd();
            if (wl.getWp() == 0){
                if (wl.getDiscounttype() == 2){ //判断折扣类型
                    yuguprice=(wl.getFwp()+(acw-wl.getFww())* wl1.getWp())+wl.getDiscount();
                }else if (wl.getDiscounttype() == 1) {
                    yuguprice=(wl.getFwp()+(acw-wl.getFww())* wl1.getWp())*wl.getDiscount();
                }

            }else if (acw>=wd1){
                yuguprice=acw*wl.getWp();

            }else {
                if (wlps.size() == 3) {
                    yuguprice = wl.getFwp() + (acw - wlps.get(0).getWu()) * wl.getWp();
                }else if(wlps.size() > 3){ //当价格段大于3，现在的价格段是3段，这段代码用于以后增加价格段
                    for(int i=0;wlps.size()>i;i++){
                        if (wl.getFw() == 1){ //首重
                            yuguprice=wl.getFwp();
                        }else {
                            if (acw > wl.wu){
                                yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                            }else { //计算到最后的价格段，此时要加上折扣
                                if (wl.getDiscounttype() == 2){ //判断折扣类型
                                    yuguprice=yuguprice+(acw-wl.wd)*wl.wp+wl.getDiscount();
                                }else if (wl.getDiscounttype() == 1) {
                                    yuguprice=(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                                }
                                break;
                            }

                        }
                    }

                }
            }
            yuguprice= round(yuguprice);//最后价格四舍五入

        }else if (wlps.get(0).getLogicName().equals("京东通用")){
            for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){
                    if(acw<wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    if (wl.getDiscounttype() == 2){ //判断折扣类型
                        yuguprice=wl.getFwp()+wl.getDiscount();
                    }else if (wl.getDiscounttype() == 1) {
                        yuguprice=wl.getFwp()*wl.getDiscount();
                    }
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else {
                        yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                        break;
                    }

                }

            }

            yuguprice= round(yuguprice);//最后价格四舍五入
        }else if (wlps.get(0).getLogicName().equals("默认")){
            for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){
                    if(acw<wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else {
                        yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                        break;
                    }

                }

            }

            if (wlps.get(0).getDiscounttype() == 2){ //判断折扣类型
                yuguprice= yuguprice+wlps.get(0).getDiscount();//最后价格四舍五入
            }else if (wlps.get(0).getDiscounttype() == 1) {
                yuguprice= yuguprice*wlps.get(0).getDiscount();//最后价格四舍五入
            }

            yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留2位小数
        }
        else { //剩下快递公司
            /*for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){ //首重
                    if(acw<=wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else { //计算到最后的价格段，此时要加上折扣
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=yuguprice+(acw-wl.wd)*wl.wp+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                        }
                        break;
                    }

                }*/
            //打折前先取整
                /*if (wl.getFw() == 1){ //首重
                    if(acw<=wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=round(wl.getFwp())+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=round(wl.getFwp())*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else { //计算到最后的价格段，此时要加上折扣
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=round(yuguprice+(acw-wl.wd)*wl.wp)+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=round(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                        }
                        break;
                    }

                }
            }
            yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留一位小数*/
            for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){
                    if(acw<wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else {
                        yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                        break;
                    }

                }

            }

            if (wlps.get(0).getDiscounttype() == 2){ //判断折扣类型
                yuguprice= yuguprice+wlps.get(0).getDiscount();//最后价格四舍五入
            }else if (wlps.get(0).getDiscounttype() == 1) {
                yuguprice= yuguprice*wlps.get(0).getDiscount();//最后价格四舍五入
            }

            yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留2位小数
        }

        System.out.println(name+"预估运费:"+yuguprice );

        return yuguprice;
    }


    public double getyugu(List<wuliupojo> wlps,double length, double wight, double height, double asw,int lctparabolic,String name){
        if (wlps.size() == 0){
            return -1.0;
        }//没有这条快递路线

        if(wlps.get(0).getFwp() == 0.0){
            return 0.0;
        }; //没有首重价格



        double volume; //体积
        double volumeWight; //体积重量
        double chargeableWeight; //计费重量
        double acw = 0; //实际计费重量

        /*体积*/
        volume=new BigDecimal(length*wight*height).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
        System.out.println("体积："+volume);

        /*体积重量*/
        volumeWight= new BigDecimal(
                //((volume/Cast_light_coefficient)/1000)
                new BigDecimal(new BigDecimal(volume).divide(new BigDecimal(lctparabolic),4,BigDecimal.ROUND_HALF_UP).doubleValue())
                        .divide(new BigDecimal(1000),4,BigDecimal.ROUND_HALF_UP)
                        .doubleValue()
        ).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println("体积重量："+volumeWight);

        /*计费重量*/
        chargeableWeight=max(volumeWight, asw);
        System.out.println("计费重量："+chargeableWeight);

        /*实际计费重量*/
        if (name.equals("顺丰纯重特配") ||name.equals("顺丰陆运") || name.equals("顺丰标快") || name.equals("顺丰干配")){
            System.out.println(name);
            if (chargeableWeight >= 100){
                acw = round(chargeableWeight);
                System.out.println("实际计费重量："+acw);

            }else if (chargeableWeight >=10){
                //IF(K4>=10,ROUND(K4*2,0)/2,ROUND(K4,1))
                acw = new BigDecimal(round(chargeableWeight*2)).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP).doubleValue();//除
                System.out.println("实际计费重量："+acw);

            }else {
                acw = new BigDecimal(chargeableWeight).setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留一位小数
                System.out.println("实际计费重量："+acw);

            }
        }else if (name.equals("京东特惠送" ) || name.equals("京东特快送")){
            System.out.println(name);
            double zh = ceil(chargeableWeight); //向上取整
            double yu = zh - chargeableWeight;  //取到小数
            //System.out.println(new BigDecimal(zh).subtract(new BigDecimal(chargeableWeight)).doubleValue());
            System.out.println("向上区间："+yu);
            if (yu > 0.5){
                yu = 0.5;
                double fl = floor(chargeableWeight); //向下取整
                acw = fl + yu;  //向上取得0.5的倍数
                System.out.println("实际计费重量："+acw);
            }else if (yu < 0.5){
                acw = zh;
                System.out.println("实际计费重量："+acw);
            }else if (yu == 0.5){
                acw = chargeableWeight;
                System.out.println("实际计费重量："+acw);
            }

        }else {
            acw = round(chargeableWeight);
            System.out.println("实际计费重量："+acw);
        }
        double yuguprice = 0;

        if (name.equals("顺丰标快")){

            wuliupojo wl = wlps.get(wlps.size()-1);
            wuliupojo wl1 = wlps.get(wlps.size()-2);
            double wd1 = wl.getWd();
            if (wl.getWp() == 0){
                if (wl.getDiscounttype() == 2){ //判断折扣类型
                    yuguprice=(wl.getFwp()+(acw-wl.getFww())* wl1.getWp())+wl.getDiscount();
                }else if (wl.getDiscounttype() == 1) {
                    yuguprice=(wl.getFwp()+(acw-wl.getFww())* wl1.getWp())*wl.getDiscount();
                }

            }else if (acw>=wd1){
                yuguprice=acw*wl.getWp();

            }else {
                if (wlps.size() == 3) {
                    yuguprice = wl.getFwp() + (acw - wlps.get(0).getWu()) * wl.getWp();
                }else if(wlps.size() > 3){ //当价格段大于3，现在的价格段是3段，这段代码用于以后增加价格段
                    for(int i=0;wlps.size()>i;i++){
                        if (wl.getFw() == 1){ //首重
                            yuguprice=wl.getFwp();
                        }else {
                            if (acw > wl.wu){
                                yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                            }else { //计算到最后的价格段，此时要加上折扣
                                if (wl.getDiscounttype() == 2){ //判断折扣类型
                                    yuguprice=yuguprice+(acw-wl.wd)*wl.wp+wl.getDiscount();
                                }else if (wl.getDiscounttype() == 1) {
                                    yuguprice=(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                                }
                                break;
                            }

                        }
                    }

                }
            }
            yuguprice= round(yuguprice);//最后价格四舍五入

        }else if (name.equals("顺丰纯重特配") || name.equals("顺丰陆运")){
            for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){
                    if(acw<wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    if (wl.getDiscounttype() == 2){ //判断折扣类型
                        yuguprice=wl.getFwp()+wl.getDiscount();
                    }else if (wl.getDiscounttype() == 1) {
                        yuguprice=wl.getFwp()*wl.getDiscount();
                    }
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else {
                        yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                        break;
                    }

                }

            }

            yuguprice= round(yuguprice);//最后价格四舍五入
        }else if (name.equals("德邦精准重货") || name.equals("德邦大件快递360")){
            for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){
                    if(acw<wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                        yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else {
                        yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                        break;
                    }

                }

            }

            if (wlps.get(0).getDiscounttype() == 2){ //判断折扣类型
                yuguprice= yuguprice+wlps.get(0).getDiscount();//最后价格四舍五入
            }else if (wlps.get(0).getDiscounttype() == 1) {
                yuguprice= yuguprice*wlps.get(0).getDiscount();//最后价格四舍五入
            }

            yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留2位小数
        }
        else { //剩下快递公司
            /*for (wuliupojo wl:wlps
            ) {
                if (wl.getFw() == 1){ //首重
                    if(acw<=wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=wl.getFwp()+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=wl.getFwp()*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else { //计算到最后的价格段，此时要加上折扣
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=yuguprice+(acw-wl.wd)*wl.wp+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                        }
                        break;
                    }

                }*/
                //打折前先取整
                /*if (wl.getFw() == 1){ //首重
                    if(acw<=wl.getFww()){
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=round(wl.getFwp())+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=round(wl.getFwp())*wl.getDiscount();
                        }
                        break;
                    }
                    yuguprice=wl.getFwp();
                }else {
                    if (acw > wl.wu){
                        yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                    }else { //计算到最后的价格段，此时要加上折扣
                        if (wl.getDiscounttype() == 2){ //判断折扣类型
                            yuguprice=round(yuguprice+(acw-wl.wd)*wl.wp)+wl.getDiscount();
                        }else if (wl.getDiscounttype() == 1) {
                            yuguprice=round(yuguprice+(acw-wl.wd)*wl.wp)*wl.getDiscount();
                        }
                        break;
                    }

                }
            }
            yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留一位小数*/
                for (wuliupojo wl:wlps
                ) {
                    if (wl.getFw() == 1){
                        if(acw<wl.getFww()){
                            if (wl.getDiscounttype() == 2){ //判断折扣类型
                                yuguprice=wl.getFwp()+wl.getDiscount();
                            }else if (wl.getDiscounttype() == 1) {
                                yuguprice=wl.getFwp()*wl.getDiscount();
                            }
                            break;
                        }
                        yuguprice=wl.getFwp();
                    }else {
                        if (acw > wl.wu){
                            yuguprice=yuguprice+(wl.wu-wl.wd)*wl.wp;
                        }else {
                            yuguprice=yuguprice+(acw-wl.wd)*wl.wp;
                            break;
                        }

                    }

                }

                if (wlps.get(0).getDiscounttype() == 2){ //判断折扣类型
                    yuguprice= yuguprice+wlps.get(0).getDiscount();//最后价格四舍五入
                }else if (wlps.get(0).getDiscounttype() == 1) {
                    yuguprice= yuguprice*wlps.get(0).getDiscount();//最后价格四舍五入
                }

                yuguprice = new BigDecimal(yuguprice).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入 保留2位小数
        }

        System.out.println(name+"预估运费:"+yuguprice );

        return yuguprice;
    }

}
